package com.sanpu.kxe.business.main.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.HomeCenterOutput;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：全部应用适配器
 * Created by 薛勇军 on 2019/3/12.
 */

public class AllLinkAdapter extends BaseAdapter {

    private Context mContext;

    private List<HomeCenterOutput> centerOutputs = new ArrayList<>();

    public AllLinkAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setNotifyData(List<HomeCenterOutput> outputs) {
        centerOutputs.clear();
        if (outputs != null) {
            centerOutputs.addAll(outputs);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (centerOutputs == null)
            return 0;
        return centerOutputs.size();
    }

    @Override
    public HomeCenterOutput getItem(int position) {
        if (centerOutputs == null)
            return null;
        return centerOutputs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_option, null);
        }
        TextView titleTv = ButterKnife.findById(convertView, R.id.title);
        ImageView iconView = ButterKnife.findById(convertView, R.id.icon);

        HomeCenterOutput centerOutput = getItem(position);
        if (centerOutput != null) {
            if (!TextUtils.isEmpty(centerOutput.getTitle())) {
                titleTv.setText(centerOutput.getTitle());
            }
            JKImageLoader.getInstance().displayImage(
                    mContext, UrlConstants.getBaseUrl() + centerOutput.getImage(), iconView);
        }
        return convertView;
    }
}
